<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2238 – Redirecting to/from command name instead of file. Did you want pipes/xargs (or quote to ignore)?</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2238 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2238">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="redirecting-tofrom-command-name-instead-of-file-did-you-want-pipesxargs-or-quote-to-ignore">Redirecting
to/from command name instead of file. Did you want pipes/xargs (or quote
to ignore)?</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2238.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span> file <span class="op">&gt;</span> tr <span class="at">-d</span> <span class="st">&#39;\r&#39;</span></span>
<span id="cb1-2"><a href="SC2238.html#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span> file <span class="op">&gt;</span> rm</span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2238.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span> file <span class="kw">|</span> <span class="fu">tr</span> <span class="at">-d</span> <span class="st">&#39;\r&#39;</span>         <span class="co"># tr reads stdin</span></span>
<span id="cb2-2"><a href="SC2238.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span> file <span class="kw">|</span> <span class="fu">xargs</span> <span class="at">-d</span> <span class="st">&#39;\n&#39;</span> rm   <span class="co"># rm reads arguments</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>You are using file redirection, but the filename is an unquoted
command name. Instead of running the command and feeding data to it,
this just writes to a file with the same name.</p>
<p>To run the command and feed data to it, determine how it gets its
data:</p>
<ul>
<li>If the command reads from STDIN, simply use a pipe as in the first
example.</li>
<li>If the command reads multiple arguments, use a pipe to
<code>xargs</code> as in the second example</li>
</ul>
<p>Note that <code>xargs</code> has many pitfalls when it comes to
spaces and quotes. <code>cat file | xargs rm</code> will appear to work
during testing, but fails for filenames like <code>My File.txt</code> or
<code>Can't_Fight_This_Feeling.mp3</code>. The example uses the GNU
extension <code>-d '\n'</code> to more safely handle these names.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>If you actually did want to write a file named after a command,
simply quote the filename to let ShellCheck know you meant it literally
and not as a command name. This does not change anything about how the
script works:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2238.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Write to a file literally named &#39;rm&#39;, does not try to delete anything</span></span>
<span id="cb3-2"><a href="SC2238.html#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> <span class="st">&quot;A potentially dangerous command&quot;</span> <span class="op">&gt;</span> <span class="st">&quot;rm&quot;</span> </span></code></pre></div>
<h3 id="related-resources">Related resources:</h3>
<ul>
<li>Help by adding links to BashFAQ, StackOverflow, man pages, POSIX,
etc!</li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


